package com.cinema.dao.imp;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.cinema.Bean.Admin;
import com.cinema.Bean.Hall;
import com.cinema.Bean.Movie;
import com.cinema.Bean.User;
import com.cinema.dao.BaseDao;
import com.cinema.dao.IHallDao;
import com.mysql.jdbc.PreparedStatement;

/**
 * 用户表的mysql数据库实现类
 * **/

public class HallDaoMysql extends BaseDao implements IHallDao {
	public int insert(Hall a) {
		String sql = "insert into hall values(?,?)";
		String[] args = { a.getHallId(), a.getHallName()};
		return change(sql, args);
	}

	public int delete(Hall a) {
		String sql = "delete from hall where hallId=?";
		String[] args = { a.getHallId() };
		return change(sql, args);
	}

	public int update(Hall a) {
		String sql = "update hall set hallName=? where hallId=?";
		String[] args = { a.getHallName() ,a.getHallId()};
		return change(sql, args);
	}

	/**
	 * 根据Id进行查询
	 */
	public Hall SelectById(String id) {
		// TODO Auto-generated method stub
		Hall a = null;
		String sql = "select * from hall where hallId=?";
		Connection con = getCon();
		try {
			PreparedStatement pstmt = (PreparedStatement) con.prepareStatement(sql);
			pstmt.setString(1, id);
			ResultSet rs = pstmt.executeQuery();
			if (rs.next()) {
				a = new Hall();
				a.setHallId(rs.getString("hallId"));
				a.setHallName(rs.getString("hallName"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return a;
	}

	/**
	 * 获取所有
	 */
	public List<Hall> selectAll() {
		// TODO Auto-generated method stub
		List<Hall> list = new ArrayList<Hall>();
		Hall a = null;
		String sql = "select * from hall";
		Connection con = getCon();
		try {
			PreparedStatement pstmt = (PreparedStatement) con
					.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				a = new Hall();
				a.setHallId(rs.getString("hallId"));
				a.setHallName(rs.getString("hallName"));
				list.add(a);
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return list;
	}
	
	/**
	 * 根据关键词进行查询
	 */
	public List<Hall> Select(String keyword) {
		// TODO Auto-generated method stub
		List<Hall> list = new ArrayList<Hall>();
		Hall u = null;
		String sql = "select * from hall where hallId like '%" + keyword
		+ "%' or hallName like '%" + keyword + "%'";
		Connection con = getCon();
		try {
			PreparedStatement pstmt = (PreparedStatement) con
					.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				u = new Hall();
				u.setHallId(rs.getString("hallId"));
				u.setHallName(rs.getString("hallName"));
				list.add(u);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 根据传入条件进行查询
	 */
	public List<Hall> SelectByWhere(String where) {
		// TODO Auto-generated method stub
		List<Hall> list = new ArrayList<Hall>();
		Hall a = null;
		String sql = "select * from hall where 1=1 "; // where字句
		if(where!=null && !where.trim().equals("")) {
			sql+="and "+where;
		}
		Connection con = getCon();
		try {
			PreparedStatement pstmt = (PreparedStatement) con
					.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				a = new Hall();
				a.setHallId(rs.getString("hallId"));
				a.setHallName(rs.getString("hallName"));
				list.add(a);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
}
